home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / unarced / utilities / system / intuition / shadow / examples / swri / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-17  |  10.6 KB  |  396 lines

  1. /*
  2.  * SWRI Random Input Program.
  3.  *
  4.  * © Copyright 1991, ALl Rights Reserved
  5.  *
  6.  * David C. Navas
  7.  */
  8. #include <shadow/semaphore.h>
  9. #include "control.h"
  10. #include "/Gui/gui.h"
  11.  
  12. #include <ipc.h>
  13. #include <shadow/shadow_proto.h>
  14. #include <shadow/shadow_pragmas.h>
  15. #include <dos/dostags.h>
  16. #include <exec/lists.h>
  17.  
  18. #include <math.h>
  19.  
  20.  
  21. extern struct ExecBase * __far SysBase;
  22. struct IPCBase * __far IPCBase;
  23. struct ShadowBase * __far ShadowBase;
  24. struct DosLibrary * __far DOSBase;
  25.  
  26. struct Task * __far programTask;
  27. long __far GlobalNumOpen = 0;
  28.  
  29. #define MYLOCALINPUTWINDOW "Input Window Class"
  30.  
  31. extern METHOD_REF REF_IWinInitMethod[];
  32.  
  33. void IWinRemoveMethod(METHOD_ARGS),
  34.      __saveds IWinDataMethod(METHOD_ARGS),
  35.      IWinBreakMethod(METHOD_ARGS);
  36.  
  37. BOOL IWinInitMethod(METHOD_ARGS, CLUSTER cluster);
  38.  
  39. #define MYPATCHES "Patch me up!"
  40.  
  41. struct MyLocals {
  42.    COMPOSITE ml_composite;
  43. };
  44.  
  45. ATTRIBUTE_TAG IWinAttrs[] =
  46.                         {
  47.                            {
  48.                               MYPATCHES,
  49.                               sizeof(struct MyLocals),
  50.                               NULL
  51.                            },
  52.                            TAG_END
  53.                         };
  54.  
  55. METHOD_TAG IWinBreak =
  56.                         {
  57.                            METHOD_META_REMOVE,
  58.                            NULL, NULL,
  59.                            SHADOW_MSG_SYNC,
  60.                            METHOD_FLAG_PROC, 1,
  61.                            (METHODFUNCTYPE)IWinBreakMethod, NULL
  62.                         };
  63.  
  64. METHOD_TAG IWinMethods[] =
  65.                         {
  66.                            {
  67.                               METHOD_META_REMOVE,
  68.                               NULL, NULL,
  69.                               SHADOW_MSG_SYNC,
  70.                               METHOD_FLAG_PROC, 0,
  71.                               (METHODFUNCTYPE)IWinRemoveMethod, NULL
  72.                            },
  73.                            {
  74.                               METHOD_META_INIT,
  75.                               NULL, NULL,
  76.                               SHADOW_MSG_SYNC,
  77.                               METHOD_FLAG_PROC, 0,
  78.                               (METHODFUNCTYPE)IWinInitMethod,
  79.                                  REF_IWinInitMethod
  80.                            },
  81.                            {
  82.                               "data",
  83.                               NULL, NULL,
  84.                               SHADOW_MSG_FORCE_ASYNC,
  85.                               METHOD_FLAG_PROC, 0,
  86.                               (METHODFUNCTYPE)IWinDataMethod, NULL
  87.                            },
  88.                            TAG_END
  89.                         };
  90.  
  91. void main(void);
  92.  
  93. int CXBRK(void)
  94. {
  95.    return(0);
  96. }
  97.  
  98. chkabort(void)
  99. {
  100.    return(0);
  101. }
  102.  
  103. void loadControl(CLUSTER cluster);
  104.  
  105. #define PROGRAMNAME "SWRINDE Input Random"
  106.  
  107. void main()
  108. {
  109.    CLUSTER controlCluster;
  110.  
  111.    programTask = FindTask(NULL);
  112.  
  113.    if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))
  114.    {
  115.       if (IPCBase = OpenLibrary("ppipc.library", 0))
  116.       {
  117.          if (ShadowBase = (struct ShadowBase *)
  118.                            OpenLibrary("shadow.library", 4))
  119.          {
  120.             if (controlCluster = FindJazzCluster(SWRINDE_CONTROL_CLUSTER))
  121.             {
  122.                if (InitOOProgram(PROGRAMNAME))
  123.                {
  124.                   loadControl(controlCluster);
  125.  
  126.                   DropObject(controlCluster);
  127.                   RemoveCurrentProgram(NULL);
  128.                } else
  129.                {
  130.                   DropObject(controlCluster);
  131.                   VPrintf("Coult not init your program\n", NULL);
  132.                }
  133.  
  134.             } else
  135.                VPrintf("Hey -- you need to run the control program first!\n", NULL);
  136.  
  137.             CloseLibrary(ShadowBase);
  138.          }
  139.          else
  140.             VPrintf("requires shadow.library V4.3 in libs:\n", NULL);
  141.  
  142.          CloseLibrary(IPCBase);
  143.       }
  144.       else
  145.          VPrintf("requires ppipc.library in libs:\n", NULL);
  146.    }
  147.    else
  148.    {
  149.       DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 0);
  150.       Write(Output(), "Sorry, use 2.0\n", 15);
  151.    }
  152.  
  153.    CloseLibrary(DOSBase);
  154. }
  155.  
  156. void loadControl(CLUSTER cluster)
  157. {
  158.    CLASS    guiProcClass;
  159.    OBJECT   guiProcObject;
  160.    int success;
  161.  
  162.    PSemString(PROGRAMNAME, SHADOW_EXCLUSIVE_SEMAPHORE);
  163.  
  164.    /*
  165.     * Have we already started?
  166.     */
  167.    if (CreateInstance(NULL, MYLOCALINPUTWINDOW, METACLASS,
  168.                                                 cluster, METHOD_END))
  169.    {
  170.       VSemString(PROGRAMNAME);
  171.       return;
  172.    }
  173.  
  174.    /*
  175.     * No input, so startup all our classes
  176.     */
  177.    guiProcClass = FindJazzClass(GUIPROCESSCLASS);
  178.    guiProcObject = FindStringInWatchedBinTree((W_AVLTREE)
  179.                                 FindAttribute(guiProcClass, ATTR_OBJECTLIST),
  180.                                 GUITASK);
  181.  
  182.    SetupMethodTags(IWinMethods, guiProcObject, (void *)-1);
  183.  
  184.    /*
  185.     * Can't use SetupMethodTags() for these patches because the
  186.     *  MethodTag is not NULL terminated.
  187.     * So setup our patches directly.
  188.     */
  189.    IWinBreak.mtag_procObject = IWinMethods->mtag_procObject;
  190.    IWinBreak.mtag_defnObject = IWinMethods->mtag_defnObject;
  191.  
  192.    IWinMethods[2].mtag_procObject = IWinMethods[2].mtag_defnObject;
  193.  
  194.    DropObject(guiProcClass);
  195.    DropObject(guiProcObject);
  196.  
  197.    /*
  198.     * We want the class to disappear FIRST, before anything else.
  199.     *  So we use the highest priority we have, which is 1.
  200.     * iwinClass is TRANSFERRED to the program's resource tree.
  201.     */
  202.    success = AddAutoResource(NULL,
  203.                              CreateSubClass(NULL,
  204.                                             WINDOWCLASS,
  205.                                             METACLASS,
  206.                                             MYLOCALINPUTWINDOW,
  207.                                             NULL,
  208.                                             IWinAttrs,
  209.                                             IWinMethods,
  210.                                             METHOD_END),
  211.                              (char *)1);
  212.  
  213.    /*
  214.     * Class is defined - program has successfully started.
  215.     */
  216.    VSemString(PROGRAMNAME);
  217.  
  218.    /*
  219.     * startup our window.
  220.     */
  221.    success &= (BOOL)CreateInstance(NULL,
  222.                                    MYLOCALINPUTWINDOW,
  223.                                    METACLASS,
  224.                                    cluster,
  225.                                    METHOD_END);
  226.  
  227.    {
  228.       META        ctrlWindowClass;
  229.       W_AVLTREE   wbt;
  230.  
  231.       ctrlWindowClass = FindJazzClass(CONTROLWINCLASS);
  232.  
  233.  
  234.       /*
  235.        * Add Patches.  Patches must disappear before the windows,
  236.        *  so we add them at a high priority (2).  Remember that Resources
  237.        *  in AVLTree ordering have high priorities in low numbers and
  238.        *  vice-versa
  239.        */
  240.  
  241.       /*
  242.        * Patch control window, so that we get notified when the window
  243.        *  goes away.
  244.        */
  245.       success &= AddAutoResource(NULL,
  246.                                  CreateInstance(NULL,
  247.                                                 PATCHERCLASS,
  248.                                                 METACLASS,
  249.                                                 &IWinBreak,
  250.                                                 ctrlWindowClass,
  251.                                                 METHOD_END),
  252.                                  (char *)2);
  253.       DropObject(ctrlWindowClass);
  254.  
  255.       /*
  256.        * Oops, is the window already gone?
  257.        */
  258.       if (wbt = FindAttribute(ctrlWindowClass, ATTR_OBJECTLIST))
  259.       {
  260.          if (!wbt->wv_value)
  261.             success = FALSE;
  262.       } else
  263.          success = FALSE;
  264.  
  265.    }
  266.  
  267.    if (success)
  268.       HandleMessages(programTask);
  269. }
  270.  
  271. /*
  272.  * IWin Class methods.
  273.  */
  274. METHOD_REF  REF_IWinInitMethod[] = {
  275.                                       {
  276.                                          'JOBJ',
  277.                                          sizeof(void *),
  278.                                          SHADOW_CLUSTER
  279.                                       },
  280.                                       { TAG_END, 0, 0}
  281.                                    };
  282. BOOL IWinInitMethod(METHOD_ARGS, CLUSTER cluster)
  283. {
  284.    struct TagItem tag[10];
  285.  
  286.    tag[0].ti_Tag = WA_Width;
  287.    tag[0].ti_Data = 150;
  288.    tag[1].ti_Tag = WA_Height;
  289.    tag[1].ti_Data = 100;
  290.    tag[2].ti_Tag = WA_MinWidth;
  291.    tag[2].ti_Data = 60;
  292.    tag[3].ti_Tag = WA_MinHeight;
  293.    tag[3].ti_Data = 40;
  294.    tag[4].ti_Tag = WA_MaxWidth;
  295.    tag[4].ti_Data = -1;
  296.    tag[5].ti_Tag = WA_MaxHeight;
  297.    tag[5].ti_Data = -1;
  298.    tag[6].ti_Tag = WA_SimpleRefresh;
  299.    tag[6].ti_Data = TRUE;
  300.    tag[7].ti_Tag = WA_Left;
  301.    tag[7].ti_Data = 0;
  302.    tag[8].ti_Tag = WA_Top;
  303.    tag[8].ti_Data = 100;
  304.    tag[9].ti_Tag = TAG_END;
  305.  
  306.  
  307.    object = DoJazzMethod(object, class->meta_superClass, METHOD_META_INIT,
  308.                                            NULL,
  309.                                            "INPUT CONTROL PANEL",
  310.                                            NULL,
  311.                                            NULL,
  312.                                            tag, METHOD_END);
  313.  
  314.    {
  315.       struct MyLocals *mls;
  316.       void *args[4];
  317.  
  318.       if (mls = FindAttribute(object, MYPATCHES))
  319.       {
  320.          mls->ml_composite = CreateInstance(cluster, NULL, NULL,
  321.                                             METHOD_END);
  322.       }
  323.       args[0] = object;
  324.       args[1] = object->cob_class;
  325.       args[2] = "data";
  326.       args[3] = METHOD_END;
  327.  
  328.       DJM(args, SHADOW_MSG_FINDMETHOD);
  329.    }
  330.    if (AddAutoResource(programTask->tc_UserData, object, NULL))
  331.    {
  332.       GlobalNumOpen++;
  333.       return TRUE;
  334.    }
  335.    return FALSE;
  336. }
  337.  
  338. void IWinRemoveMethod(METHOD_ARGS)
  339. {
  340.    struct MyLocals *mls;
  341.  
  342.    DropObject(RemoveAutoResource(programTask->tc_UserData, object, NULL));
  343.  
  344.    /*
  345.     * Let Input know not to keep sending data out.
  346.     */
  347.  
  348.    mls = FindAttribute(object, MYPATCHES);
  349.  
  350.    RemoveObject(SetObject(&mls->ml_composite, NULL));
  351.  
  352.    CallSuper();               /* Let GadTools know to get rid of gadgets. */
  353.    if (!--GlobalNumOpen)
  354.       Signal(programTask, SIGBREAKF_CTRL_C);
  355. }
  356.  
  357. void IWinBreakMethod(METHOD_ARGS)
  358. {
  359.    Signal(programTask, SIGBREAKF_CTRL_C);
  360. }
  361.  
  362. void __saveds IWinDataMethod(METHOD_ARGS)
  363. {
  364.    COMPOSITE comp;
  365.    struct MyLocals *mls;
  366.    OBJECT    objectComp;
  367.    double *table;
  368.    int i, j;
  369.  
  370.    mls = FindAttribute(object, MYPATCHES);
  371.    comp = (COMPOSITE)GetObject(&mls->ml_composite);
  372.  
  373.    /*
  374.     * Input window removal NULLs out this field.
  375.     */
  376.    if (!comp)
  377.       return;
  378.  
  379.    objectComp = FindStringInBinTree(&comp->cc_classes, SWRINDE_DATA_CLASS);
  380.    table = FindAttribute(objectComp, ATTR_SWRINDE_DATA);
  381.  
  382.    for (j = 0; j <10 && !(SetSignal(NULL, NULL) & SIGBREAKF_CTRL_C); j++)
  383.    {
  384.       for (i = 0; i < 256; i++)
  385.       {
  386.          table[i] = drand48();
  387.       }
  388.       DoJazzMethod(comp, NULL, METHOD_SWRINDECONTROLCLUSTER_COMPUTE,
  389.                                METHOD_END);
  390.    }
  391.    DropObject(objectComp);
  392.    DropObject(comp);
  393.  
  394.    DJM((void **)&object, NULL);
  395. }
  396.